package com.self.task;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

public class ThPool {

    private ScheduledExecutorService scheduledExecutorService;

    public void loopTask(Runnable command,long interval,long delay) {

        if(scheduledExecutorService == null) {
            scheduledExecutorService = Executors.newScheduledThreadPool(1, new ThreadFactory() {

                @Override
                public Thread newThread(Runnable r) {
                    Thread thread = new Thread(r);
                    thread.setDaemon(true);
                    return thread;
                }
            });
        }

        scheduledExecutorService.scheduleAtFixedRate(command,delay,interval, TimeUnit.NANOSECONDS);
    }
}
